package com.example.blog.modules.user.controller;

import com.example.blog.common.api.ApiResponse;
import com.example.blog.common.constant.Permissions;
import com.example.blog.common.security.HasPermission;
import com.example.blog.modules.log.annotation.Log;
import com.example.blog.modules.user.dto.RoleDTO;
import com.example.blog.modules.user.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Tag(name = "角色接口", description = "角色管理相关接口")
@RestController
@RequestMapping("/api/roles")
@RequiredArgsConstructor
@SecurityRequirement(name = "bearerAuth")
public class RoleController {
    
    private final RoleService roleService;
    
    @Operation(summary = "创建角色")
    @PostMapping
    @HasPermission(Permissions.ROLE_CREATE)
    @Log(value = "创建角色", operation = "CREATE")
    public ApiResponse<RoleDTO> createRole(@Valid @RequestBody RoleDTO roleDTO) {
        return ApiResponse.success(roleService.createRole(roleDTO));
    }
    
    @Operation(summary = "更新角色")
    @PutMapping("/{id}")
    @HasPermission(Permissions.ROLE_UPDATE)
    @Log(value = "更新角色", operation = "UPDATE")
    public ApiResponse<RoleDTO> updateRole(@PathVariable Long id, @Valid @RequestBody RoleDTO roleDTO) {
        return ApiResponse.success(roleService.updateRole(id, roleDTO));
    }
    
    @Operation(summary = "删除角色")
    @DeleteMapping("/{id}")
    @HasPermission(Permissions.ROLE_DELETE)
    @Log(value = "删除角色", operation = "DELETE")
    public ApiResponse<Void> deleteRole(@PathVariable Long id) {
        roleService.deleteRole(id);
        return ApiResponse.success();
    }
    
    @Operation(summary = "获取角色信息")
    @GetMapping("/{id}")
    @HasPermission(Permissions.ROLE_VIEW)
    public ApiResponse<RoleDTO> getRole(@PathVariable Long id) {
        return ApiResponse.success(roleService.getRoleById(id));
    }
    
    @Operation(summary = "获取所有角色")
    @GetMapping
    @HasPermission(Permissions.ROLE_VIEW)
    public ApiResponse<List<RoleDTO>> getAllRoles() {
        return ApiResponse.success(roleService.getAllRoles());
    }
    
    @Operation(summary = "分配角色给用户")
    @PostMapping("/users/{userId}/assign")
    @HasPermission(Permissions.ROLE_UPDATE)
    @Log(value = "分配用户角色", operation = "UPDATE")
    public ApiResponse<Void> assignRolesToUser(
            @PathVariable Long userId,
            @RequestBody Set<String> roleNames) {
        roleService.assignRolesToUser(userId, roleNames);
        return ApiResponse.success();
    }
    
    @Operation(summary = "移除用户的角色")
    @PostMapping("/users/{userId}/remove")
    @HasPermission(Permissions.ROLE_UPDATE)
    @Log(value = "移除用户角色", operation = "UPDATE")
    public ApiResponse<Void> removeRolesFromUser(
            @PathVariable Long userId,
            @RequestBody Set<String> roleNames) {
        roleService.removeRolesFromUser(userId, roleNames);
        return ApiResponse.success();
    }
    
    @Operation(summary = "获取用户的角色")
    @GetMapping("/users/{userId}/roles")
    @HasPermission(Permissions.ROLE_VIEW)
    public ApiResponse<Set<String>> getUserRoles(@PathVariable Long userId) {
        return ApiResponse.success(roleService.getUserRoles(userId));
    }
    
    @Operation(summary = "获取用户的权限")
    @GetMapping("/users/{userId}/permissions")
    @HasPermission(Permissions.ROLE_VIEW)
    public ApiResponse<Set<String>> getUserPermissions(@PathVariable Long userId) {
        return ApiResponse.success(roleService.getUserPermissions(userId));
    }
} 